﻿using System;
using GatewayBizProcess.Db;
using System.Linq;
using log4net;

namespace GatewayBizProcess.Facade
{
    public static class CommandDown
    {
        static readonly ILog Log = LogManager.GetLogger(typeof(CommandDown));
        /// <summary>
        /// 下行实时数据
        /// </summary>
        public static void Down2011()
        {
            using (var pCtx = new PlatformZrContext())
            using (var gCtx = new GatewayTotalPilotContext())
            {
                var realtimes = pCtx.data_realtime_main.Where(t => t.is_synchronize == null || t.is_synchronize == false)
                    .OrderBy(t => t.monitor_time)
                    .Take(1000)
                    .ToList();
                foreach (var realtime in realtimes)
                {
                    var now = DateTime.Now;
                    //var port = BaseInfoBuffer.GetInfo(realtime.ps_base_id, realtime.ps_pi_id);
                    var qn = now.ToString("yyyyMMddHHmmssfff");
                    var sendCpString = "QN=" + qn + ";ST=32;CN=2011;PW=123456;MN=" + realtime.mn + ";Flag=0;CP=&&DataTime=" +
                                       realtime.monitor_time.ToString("yyyyMMddHHmmss");
                    //sendCpString = Crc.WrapByMonitorCrc(sendCpString);
                    var details = pCtx.data_realtime.Where(t => t.main_id == realtime.id).ToList();
                    if (details.Count == 0) continue;
                    foreach (var detail in details)
                    {
                        sendCpString = sendCpString + ";" + detail.pollutant_code + "-Rtd=" + detail.rtd;
                    }
                    sendCpString = sendCpString + "&&";
                    send_data sendData = new send_data
                    {
                        cn = "2011",
                        is_sent = false,
                        mn = realtime.mn,
                        qn = qn,
                        send_string = sendCpString,
                        send_time = now,
                        sent_time = now
                    };
                    gCtx.send_data.Add(sendData);
                    realtime.is_synchronize = true;
                    Log.Info("根据平台生成2011命令到网关：" + sendCpString);
                }
                gCtx.SaveChanges();
                pCtx.SaveChanges();


            }
        }
        /// <summary>
        /// 下行小时数据
        /// </summary>
        public static void Down2061()
        {
            using (var pCtx = new PlatformZrContext())
            using (var gCtx = new GatewayTotalPilotContext())
            {
                var realtimes = pCtx.data_hour_main.Where(t => t.is_synchronize == null || t.is_synchronize == false)
                    .OrderBy(t => t.monitor_time)
                    .Take(1000)
                    .ToList();
                foreach (var realtime in realtimes)
                {
                    var now = DateTime.Now;
                    //var port = BaseInfoBuffer.GetInfo(realtime.ps_base_id, realtime.ps_pi_id);
                    var qn = now.ToString("yyyyMMddHHmmssfff");
                    var sendCpString = "QN=" + qn + ";ST=32;CN=2061;PW=123456;MN=" + realtime.mn + ";Flag=1;CP=&&DataTime=" +
                                       realtime.monitor_time.ToString("yyyyMMddHHmmss");
                    var details = pCtx.data_hour.Where(t => t.main_id == realtime.id).ToList();
                    if (details.Count == 0) continue;
                    var theDay =
                            pCtx.data_day_main.Where(t => t.mn.Equals(realtime.mn))
                                .OrderByDescending(t => t.monitor_time)
                                .FirstOrDefault();
                    var theMonth =
                            pCtx.data_month_main.Where(t => t.mn.Equals(realtime.mn))
                                .OrderByDescending(t => t.monitor_time)
                                .FirstOrDefault();
                    var theYear =
                            pCtx.data_year_main.Where(t => t.mn.Equals(realtime.mn))
                                .OrderByDescending(t => t.monitor_time)
                                .FirstOrDefault();

                    foreach (var detail in details)
                    {
                        sendCpString = sendCpString + ";" + detail.pollutant_code + "-Avg=" + detail.Avg;
                        sendCpString = sendCpString + "," + detail.pollutant_code + "-Cou=" + detail.Cou;
                        if (theDay != null)
                        {
                            var data = pCtx.data_day.Where(t => t.main_id == theDay.id).FirstOrDefault(t => t.pollutant_code.Equals(detail.pollutant_code));
                            if (data != null)
                                sendCpString = sendCpString + "," + detail.pollutant_code + "-Cou-Day=" + data.Cou;
                        }
                        if (theMonth != null)
                        {
                            var data = pCtx.data_month.Where(t => t.main_id == theMonth.id).FirstOrDefault(t => t.pollutant_code.Equals(detail.pollutant_code));
                            if (data != null)
                                sendCpString = sendCpString + "," + detail.pollutant_code + "-Cou-Mon=" + data.Cou;
                        }
                        if (theYear != null)
                        {
                            var data = pCtx.data_year.Where(t => t.main_id == theYear.id).FirstOrDefault(t => t.pollutant_code.Equals(detail.pollutant_code));
                            if (data != null)
                                sendCpString = sendCpString + "," + detail.pollutant_code + "-Cou-Yea=" + data.Cou;
                        }
                    }
                    sendCpString = sendCpString + "&&";
                    send_data sendData = new send_data
                    {
                        cn = "2061",
                        is_sent = false,

                        mn = realtime.mn,
                        qn = qn,
                        send_string = sendCpString,
                        send_time = now,
                        sent_time = now
                    };
                    gCtx.send_data.Add(sendData);
                    realtime.is_synchronize = true;
                    Log.Info("根据平台生成2061命令到网关：" + sendCpString);
                }
                gCtx.SaveChanges();
                pCtx.SaveChanges();
            }
        }
        /// <summary>
        /// 阀门控制
        /// </summary>
        public static void Down3051_3097()
        {
            try
            {
                using (var pCtx = new PlatformZrContext())
                using (var gCtx = new GatewayTotalPilotContext())
                {
                    var realtimes = pCtx.gateway_device_ctrl.Where(t => t.qn_rtn == null || t.exe_rtn == null)
                        .OrderBy(t => t.execute_time)
                        .Take(1000)
                        .ToList();
                    foreach (var realtime in realtimes)
                    {
                        realtime.qn_rtn = "-";
                        realtime.exe_rtn = "-";
                        if (string.IsNullOrEmpty(realtime.mn)) continue;
                        var now = DateTime.Now;
                        var qn = string.IsNullOrEmpty(realtime.qn) ? now.ToString("yyyyMMddHHmmssfff") : realtime.qn;
                        var sendCpString = "QN=" + qn + ";ST=32;CN=" + realtime.ctrl_code + ";PW=123456;MN=" +
                                           realtime.mn + ";Flag=1;CP=&&";
                        var param = Convert.ToDouble(realtime.param).ToString("0");
                        var cp = "";
                        if (realtime.ctrl_code.Length > 4)
                            realtime.ctrl_code = "8888";
                        if (realtime.ctrl_code.Equals("3051"))
                        {
                            cp = "Per=" + param;
                        }
                        else if (realtime.ctrl_code.Equals("3097"))
                        {
                            cp = "Gate=ON";
                        }
                        //if (realtime.ctrl_code.Equals("3051"))
                        //{
                        //    cp = "Per=" + param;
                        //}
                        sendCpString = sendCpString + cp + "&&";
                        send_ctr_cmd sendData = new send_ctr_cmd
                        {
                            cn = realtime.ctrl_code,
                            is_sent = false,
                            mn = realtime.mn,
                            qn = qn,
                            send_string = sendCpString,
                            send_time = now,
                            sent_time = now,
                            rtn_9012 = "",
                            rtn_9011 = "",
                            rtn_9014 = "",

                        };
                        gCtx.send_ctr_cmd.Add(sendData);
                        Log.Info("根据平台生成3051_3097命令到网关：" + sendCpString);
                    }
                    gCtx.SaveChanges();
                    pCtx.SaveChanges();
                    //send 
                }
            }
            catch (Exception ex)
            {
                Log.Error("Down3051_3097", ex);
            }

        }
        /// <summary>
        /// 下发远程反控采样
        /// </summary>
        public static void Down3012()
        {
            using (var pCtx = new PlatformZrContext())
            using (var gCtx = new GatewayTotalPilotContext())
            {
                var realtimes = pCtx.gateway_sample_take.Where(t => t.qn_rtn == null || t.exe_rtn == null)
                    .OrderBy(t => t.execute_time)
                    .Take(1000)
                    .ToList();
                foreach (var realtime in realtimes)
                {
                    realtime.qn_rtn = "-";
                    realtime.exe_rtn = "-";
                    if (string.IsNullOrEmpty(realtime.mn)) continue;
                    var now = DateTime.Now;
                    var qn = string.IsNullOrEmpty(realtime.qn) ? now.ToString("yyyyMMddHHmmssfff") : realtime.qn;
                    var sendCpString = "QN=" + qn + ";ST=32;CN=" + realtime.ctrl_code + ";PW=123456;MN=" + realtime.mn + ";Flag=1;CP=&&";
                    var cp = "";
                    if (realtime.ctrl_code.Equals("3012"))
                    {

                        cp = "BotID=" + realtime.bottle_id + ";";

                        cp = cp + "PolID" + realtime.pollutant_code;

                    }
                    sendCpString = sendCpString + cp + "&&";
                    send_ctr_cmd sendData = new send_ctr_cmd
                    {
                        cn = realtime.ctrl_code,
                        is_sent = false,

                        mn = realtime.mn,
                        qn = qn,
                        send_string = sendCpString,
                        send_time = now,
                        sent_time = now
                    };
                    gCtx.send_ctr_cmd.Add(sendData);
                    Log.Info("根据平台生成3012命令到网关：" + sendCpString);
                }
                gCtx.SaveChanges();
                pCtx.SaveChanges();
                //send 
            }
        }
        /// <summary>
        /// 下发月允许排放量
        /// </summary>
        public static void Down3093()
        {
            using (var pCtx = new PlatformZrContext())
            using (var gCtx = new GatewayTotalPilotContext())
            {
                var realtimes =
                    pCtx.biz_month_plan_main.Where(t => t.is_synchronize == null || t.is_synchronize == false).ToList();
                foreach (var realtime in realtimes)
                {
                    realtime.is_synchronize = true;
                    if (realtime.ps_base_id == null || realtime.ps_pi_id == null) continue;
                    var port = BaseInfoBuffer.GetInfo(realtime.ps_base_id.Value, realtime.ps_pi_id.Value);
                    if (string.IsNullOrEmpty(port?.mn)) continue;
                    var now = DateTime.Now;
                    var qn = now.ToString("yyyyMMddHHmmssfff");
                    var sendCpString = "QN=" + qn + ";ST=32;CN=" + "3093" + ";PW=123456;MN=" + port.mn + ";Flag=1;CP=&&";
                    if (realtime.month == null) continue;
                    var cp = "Month=" + realtime.month.Value.ToString("yyyyMM") + ";";
                    var details = pCtx.biz_month_plan.Where(t => t.main_id == realtime.id).ToList();
                    foreach (var detail in details)
                    {
                        if (detail.allow != null) cp = cp + detail.code + "-MonAll=" + detail.allow.Value.ToString("0.00") + ";";
                    }
                    sendCpString = sendCpString + cp + "&&";
                    send_ctr_cmd sendData = new send_ctr_cmd
                    {
                        cn = "3093",
                        is_sent = false,
                        mn = port.mn,
                        qn = qn,
                        send_string = sendCpString,
                        send_time = now,
                        sent_time = now
                    };
                    gCtx.send_ctr_cmd.Add(sendData);
                    Log.Info("根据平台生成3093命令到网关：" + sendCpString);
                }
                gCtx.SaveChanges();
                pCtx.SaveChanges();
                //send 
            }
        }
        /// <summary>
        /// 下发许可证污染物信息
        /// </summary>
        public static void Down3094()
        {
            using (var pCtx = new PlatformZrContext())
            using (var gCtx = new GatewayTotalPilotContext())
            {
                var realtimes =
                    pCtx.biz_licence_base_info.Where(t => t.is_synchronize == null || t.is_synchronize == false).ToList();
                foreach (var realtime in realtimes)
                {
                    realtime.is_synchronize = true;
                    if (realtime.ps_pi_id == null) continue;
                    var port = BaseInfoBuffer.GetInfo(realtime.ps_base_id, realtime.ps_pi_id.Value);
                    if (string.IsNullOrEmpty(port?.mn)) continue;
                    var now = DateTime.Now;
                    var qn = now.ToString("yyyyMMddHHmmssfff");
                    var sendCpString = "QN=" + qn + ";ST=32;CN=" + "3094" + ";PW=123456;MN=" + port.mn + ";Flag=1;CP=&&";
                    var cp = "";
                    var details = pCtx.biz_licence_pollutant_info.Where(t => t.licence_base_id == realtime.id).ToList();
                    foreach (var detail in details)
                    {
                        if (detail.day_allow != null)
                            cp = cp + detail.pollutant_code + "-DayAll=" + detail.day_allow.Value.ToString("0.00") + ";";
                        if (detail.year_allow != null)
                            cp = cp + detail.pollutant_code + "-YeaAll=" + detail.year_allow.Value.ToString("0.00") + ";";
                        if (detail.upper_limit != null)
                            cp = cp + detail.pollutant_code + "-Rtd=" + detail.upper_limit.Value.ToString("0.00") + ";";
                    }
                    sendCpString = sendCpString + cp + "&&";
                    send_ctr_cmd sendData = new send_ctr_cmd
                    {
                        cn = "3094",
                        is_sent = false,
                        mn = port.mn,
                        qn = qn,
                        send_string = sendCpString,
                        send_time = now,
                        sent_time = now
                    };
                    gCtx.send_ctr_cmd.Add(sendData);
                    Log.Info("根据平台生成3094命令到网关：" + sendCpString);
                }
                gCtx.SaveChanges();
                pCtx.SaveChanges();
                //send 
            }
        }
    }
}
